/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package session;

import entities.ResTable;
import entities.Restaurants;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author TANGQUOCTUAN
 */
@Stateless
public class ResTableFacade extends AbstractFacade<ResTable> implements ResTableFacadeLocal {
    @PersistenceContext(unitName = "eProject_Tunga_Restaurant-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public ResTableFacade() {
        super(ResTable.class);
    }

    public List showTable() {
       Query r=em.createNamedQuery("ResTable.findAll");
       return r.getResultList();
    }

    public List detailTables(Integer id) {
        Query r = em.createNamedQuery("ResTable.findByTableNumber");
        r.setParameter("tableNumber", id);
        return r.getResultList();
    }

    public boolean insertTable(int resID, int noSeats, String description, String picture) {
        Restaurants rs =em.find(Restaurants.class, resID);
        ResTable rt = new ResTable(noSeats,description,picture);
        rt.setRestaurants(rs);
        rs.getResTableList().add(rt);
        em.persist(rt);
        return true;
    }

    public boolean deleteTable(Integer id) {
        ResTable r = em.find(ResTable.class, id);
        em.remove(r);
        return true;
    }

    public boolean updateTable(int number,int noSeats, int resID, String description, String picture) {
        Restaurants rs =em.find(Restaurants.class, resID);
        ResTable r = em.find(ResTable.class,number );
        if(r==null) return false;
        r.setNoSeats(noSeats);
        r.setRestaurants(rs);
        r.setDescription(description);
        r.setPicture(picture);
        em.persist(r);
        return true;

    }

    public List getTableName() {
        Query r=em.createNamedQuery("ResTable.findAll");
        return r.getResultList();

    }
}
